package com.example.administrator.kotiln.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.administrator.kotiln.R;
import com.example.administrator.kotiln.utils.Utility;
import com.example.administrator.kotiln.zxing.activity.CaptureActivity;

/**
 * Created by Administrator on 2017/12/4 0004.
 */

public class ZxingActivity extends AppCompatActivity {
    private static final int MY_PERMISSIONS_REQUEST_CAMERA = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utility.setActionBar(this);
        setContentView(R.layout.activity_zxing);
        initView();
    }

    private void initView() {
        Button sao = (Button) findViewById(R.id.sao);

        sao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ContextCompat.checkSelfPermission(ZxingActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

                    ZxingActivity.this.requestPermissions(new String[]{Manifest.permission.CAMERA}, 1);
                } else {
                    Intent intent = new Intent(ZxingActivity.this, CaptureActivity.class);
                    startActivity(intent);
                }

            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        if (requestCode == MY_PERMISSIONS_REQUEST_CAMERA) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(ZxingActivity.this, CaptureActivity.class);
                startActivity(intent);
            } else {
//                 Permission Denied
                Toast.makeText(ZxingActivity.this, getResources().getString(R.string.union_tv_camera), Toast.LENGTH_SHORT).show();
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
